package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.domain.Travel;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.service.ITravelService;
import cn.wolfcode.luowowo.acticle.vo.DestinationStatisVO;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.acticle.vo.TravelStatisVO;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IDestinationStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IRedisService redisService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IDestinationStatisRedisService destinationStatisRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("---------------begin---------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            vo = new StrategyStatisVO();
            vo.setIsabroad(detail.isIsabroad());
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());
            vo.setStrateyId(detail.getId());
            vo.setThumbsupnum(detail.getThumbsupnum());
            vo.setSharenum(detail.getSharenum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getStartnum());
            vo.setViewnum(detail.getClicknum());
            vo.setFavornum(detail.getStartnum());
            vo.setViewnum(detail.getClicknum());
            System.out.println(vo);
            strategyStatisRedisService.setStatisVo(detail.getId(), vo);
        }

        System.out.println("----------------游记数据初始化-------------------");
        List<Travel> travels = travelService.list();
        TravelStatisVO travelStatisVO = null;
        for (Travel travel : travels) {
            travelStatisVO = new TravelStatisVO();
            travelStatisVO.setFavornum(travel.getStarnum());
            travelStatisVO.setSharenum(travel.getSharenum());
            travelStatisVO.setThumbsupnum(travel.getThumbsupnum());
            travelStatisVO.setViewnum(travel.getViewnum());
            travelStatisVO.setReplynum(travel.getReplynum());
            travelStatisRedisService.setStatisVo(travel.getId(), travelStatisVO);
        }
        System.out.println("-----------------目的地数据初始化-------------");
        DestinationStatisVO destinationStatisVO = new DestinationStatisVO();
        destinationStatisVO.setFavornum(777);
        destinationStatisVO.setSharenum(666);
        destinationStatisVO.setThumbsupnum(555);
        destinationStatisVO.setViewnum(999);
        destinationStatisVO.setReplynum(888);
        destinationStatisRedisService.setStatisVo(193L, destinationStatisVO);
        destinationStatisRedisService.setStatisVo(3261L, destinationStatisVO);
        destinationStatisRedisService.setStatisVo(3262L, destinationStatisVO);
        destinationStatisRedisService.setStatisVo(241L, destinationStatisVO);
        System.out.println("---------------国内,外攻略排行初始化---------------------");
        for (StrategyDetail detail : list) {
            int score = detail.getStartnum() + detail.getThumbsupnum();

            //判断Zset的key中是否存在当前value,不存在将初始化数据
            if (!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, "",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":" + detail.getId())) {

                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, detail.getId(), score);

            }
        }


        System.out.println("---------------热门攻略排行初始化---------------------");
        for (StrategyDetail detail : list) {

            //分数: ---> 阅读量 + 评论数
            int score = detail.getClicknum() + detail.getReplynum();

            //判断Zset的key中是否存在当前value,不存在将初始化数据
            if (!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, "",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":" + detail.getId())) {

                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, detail.getId(), score);

            }
        }


        System.out.println("---------------end---------------------");
    }

}